// localLifePackage/pages/withdrawal/withdrawal.js
var common = require("../../../common/common.js").common;

var colorParse = require("../../../common/color.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose: 1,
        money: 0,
        put_forward: "",
        service: 0,
        serviceType: 0,
        service_money: 0,
        payee_name: "",
        account1: "",
        account2: "",
        accountOwner: "",
        accountWechatOwner: "",
        accountWechatOffOwner: "",
        info: {
            balance: 0,
            wechat: 0,
            alipay: 0,
            blank: 0,
            wechatOnffline: 0
        },
        bank_name: "",
        payBtn: false
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            money: options.money
        });
        this.getBase();
        this.readInfo();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            data: {},
            success: res => {
                var copyText = [];
                if (res.extra.copyright_msg) {
                    var copy = res.extra.copyright_msg;
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                }
                var rgba = colorParse.hexToRgb(res.extra.color_main, .8);
                that.setData({
                    color_main: res.extra.color_main,
                    color_main2: rgba,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    phone: res.extra.phone,
                    copyImg: res.extra.copyright_img,
                    copyText: copyText,
                    l_supply: res.extra.l_supply,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    leader_address_on: res.extra.leader_address_on,
                    vip_card_on: res.extra.vip_card_on
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
            }
        });
    },
    /**
   * 用户点击右上角分享
   */
    // onShareAppMessage: function () {
    // },
    choose: function(e) {
        var id = e.currentTarget.dataset.id;
        this.setData({
            choose: id
        });
    },
    putmoney: function(e) {
        var money = e.detail.value;
        if (money == ".") {
            money = "0.";
            this.setData({
                put_forward: money
            });
            return;
        }
        money = money.replace(/[^\d.]/g, "").replace(/\.{2,}/g, ".").replace(".", "$#$").replace(/\./g, "").replace("$#$", ".").replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3").replace(/^\./g, "");
        this.setData({
            put_forward: money
        });
    },
    all: function(e) {
        var money = e.currentTarget.dataset.money;
        this.setData({
            put_forward: money
        });
    },
    getPayeeName: function(e) {
        this.setData({
            payee_name: e.detail.value
        });
    },
    getAccount1: function(e) {
        this.setData({
            account1: e.detail.value
        });
    },
    getAccount2: function(e) {
        this.setData({
            account2: e.detail.value
        });
    },
    getAccountOwner(e) {
        this.setData({
            accountOwner: e.detail.value
        });
    },
    getAccountWechatOwner(e) {
        this.setData({
            accountWechatOwner: e.detail.value
        });
    },
    getAccountWechatOffOwner(e) {
        this.setData({
            accountWechatOffOwner: e.detail.value
        });
    },
    getBankName(e) {
        this.setData({
            bank_name: e.detail.value
        });
    },
    putForward: function() {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        var payBtn = that.data.payBtn;
        if (payBtn) {
            return false;
        }
        var put_forward = that.data.put_forward;
        var payee_name = that.data.payee_name;
        var apply_type = that.data.choose;
        var account = "";
        var all_money = that.data.money;
        that.setData({
            payBtn: true
        });
        if (!put_forward) {
            wx.showModal({
                title: "提示",
                content: "请输入提现金额",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        if (parseFloat(put_forward) > parseFloat(all_money)) {
            wx.showModal({
                title: "提示",
                content: "提现金额超出可提现佣金！",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        var param = {};
        if (apply_type == 2) {
            //支付宝
            param.payee_name = that.data.accountOwner;
            param.account = that.data.account1;
        }
        if (apply_type == 3) {
            //银行卡
            param.bank_name = that.data.bank_name;
            param.account = that.data.account2;
            param.payee_name = that.data.payee_name;
        }
        if (apply_type == 1) {
            //微信线上
            param.payee_name = that.data.accountWechatOwner;
        }
        if (apply_type == 4) {
            //微信线下
            param.payee_name = that.data.accountWechatOffOwner;
        }
        if (param.payee_name == "" && (apply_type == 1 || apply_type == 4)) {
            wx.showModal({
                title: "提示",
                content: "请将微信信息填写完整！",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        if ((param.payee_name == "" || param.account == "") && apply_type == 2) {
            wx.showModal({
                title: "提示",
                content: "请将支付宝信息填写完整！",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        if ((param.bank_name == "" || param.account == "" || param.payee_name == "") && apply_type == 3) {
            wx.showModal({
                title: "提示",
                content: "请将银行卡信息填写完整！",
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            payBtn: false
                        });
                    }
                }
            });
            return false;
        }
        param.apply_price = put_forward;
        param.method = apply_type;
        common.request({
            endpoint: "Localbusiness/common/apply-draw",
            method: "post",
            data: param,
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                wx.showModal({
                    title: "提示",
                    content: "已提交审核，请耐心等待！"
                });
                setTimeout(() => {
                    wx.navigateBack({});
                }, 1500);
            },
            fail: res => {
                console.log(res);
                wx.showModal({
                    title: "提示",
                    content: res.message,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            that.setData({
                                payBtn: false
                            });
                        }
                    }
                });
            }
        });
    },
    readInfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Localbusiness/common/local-set",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                // var m_apply_type = '1,2,3,4';
                                var draw_way = res.extra.draw_way;
                that.dealWay(draw_way);
                that.setData({
                    infoSet: res.extra
                });
            },
            fail: res => {}
        });
    },
    dealWay: function(payWay) {
        let that = this;
        var payWay = payWay.split(",");
        let info = that.data.info;
        payWay.forEach((item, index) => {
            switch (item) {
              case "0":
                info.balance = 1;
                break;

                //余额
                              case "1":
                info.wechat = 1;
                break;

                //微信
                              case "2":
                info.alipay = 1;
                break;

                //支付宝
                              case "3":
                info.blank = 1;
                break;

                //银行卡
                              case "4":
                info.wechatOnffline = 1;
                break;
                //微信线下  
                        }
        });
        that.setData({
            info: info,
            choose: payWay[0]
        });
    }
});